<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="mall.demo.mybatis.pojo.ProductInCart"%>
<%@ page import="mall.demo.mybatis.pojo.User"%>
<%@ page import="mall.demo.util.SessionKeyEnum"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.ArrayList"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>我的购物车</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<link href="css/shoppingCart.css" rel="stylesheet" type="text/css">
<script src="js/jquery.js?_=201703042022"></script>
<script src="js/bootstrap.js"></script>
<script src="js/addCart.js"></script>
<script src="js/cutCart.js"></script>
<style type="text/css">
</style>
</head>

<%!//首先是定义计算购物车总金额的方法
	double sumUp(List<ProductInCart> productInCart) {
		double sum = 0.0;
		for (ProductInCart pc : productInCart) {
			sum += pc.getPrice() * pc.getAmount();
		}
		return sum;
	}%>

<%
	//获取购物车，如果有就列出来，没有的话就不做操作
	List<ProductInCart> productInCart = (List<ProductInCart>) request
			.getSession().getAttribute(SessionKeyEnum.cart.name());
	if (productInCart == null) {
		productInCart = new ArrayList<ProductInCart>();
	}
	Object obj = request.getSession().getAttribute(
			SessionKeyEnum.userObj.name());
	boolean isUserLoggedIn = false;
	User userInfo = null;
	if (obj != null) {
		isUserLoggedIn = true;
		userInfo = (User) obj;
	}
%>
<body>
	<div class="container">
		<div class="top">
			<div class="top_logo">
				<font id="Logo"><%=userInfo.getDisplayName() %>的购物车</font>
			</div>
			<!-- 分为登录和未登录两种情况 -->
			<div class="top_userInfo">
				<%
					if (isUserLoggedIn) {
				%>
				<b><a href="#">返回首页</a></b> <b><a
					href="order.jsp">我的订单</a></b> <b><a href="logout.do">退出登录</a></b>
				<%
					} else {
				%>
				<b><a href="register.jsp">注册</a></b> <b><a href="login.jsp">登录</a></b>
				<%
					}
				%>
			</div>
			<br>
			<hr>
		</div>
		<div class="cart_show">
			<table>
				<tr>
					<td colspan="5" class="cart_list_top">购物车清单</td>
				</tr>
				<tr>
					<td class="cart_list_infos_1">ProductCode</td>
					<td class="cart_list_infos_2">ProductName</td>
					<td class="cart_list_infos_1">ProductPrice</td>
					<td class="product_amount_control">ProductAmount</td>
					<td class="cart_list_infos_1">总金额(单位/元)</td>
				</tr>
				<%
					if(productInCart.isEmpty()) {
				 %>
				<tr>
					<td colspan="5">您的购物车空空如也!还不快去抢购<a href="index.jsp">继续购物</a></td>
				</tr>
				<%
				 	} else {
				 	for(ProductInCart pic : productInCart) {
				  %>
				<tr>
					<td class="cart_list_infos_1"><%=pic.getCode() %></td>
					<td class="cart_list_infos_2"><%=pic.getName() %></td>
					<td class="cart_list_infos_1"><%=pic.getPrice() %></td>
					<td align="center" class="product_amount_control">
						<button class="cutAmount" type="button" onclick="cutFromCart(<%=pic.getProductId()%>);">-</button>
						<%
							String amountStr = "\"" + pic.getAmount() + "\"";
						 %>
						<input id="<%=pic.getProductId() %>Amount" type="text" readOnly="true" value=<%=amountStr %>/>
						<button class="addAmount" type="button" onclick="addToCart(<%=pic.getProductId()%>);">+</button>
					</td>
					<td class="cart_list_infos_1" id="<%=pic.getProductId() %>Money"><%=pic.getAmount() * pic.getPrice() %></td>
				</tr>
				<%
				  	}
				   %>
				<tr class="cart_totalAmount">
					<td colspan="5">订单总金额:<span id="totalAmount"><%= sumUp(productInCart) %></span>元</td>
				</tr>
				<%
				   	}
				    %>
				<%
					if(isUserLoggedIn && !productInCart.isEmpty()) {
				 %>
				<tr>
					<td colspan="5">
						点击<a href="index.jsp">继续浏览商品</a>&nbsp;&nbsp;&nbsp;&nbsp;
						确定<a href="makeorder.do">下单</a>
					</td>
				</tr>
				<%
					}
				 %>
			</table>
		</div>
	</div>
</body>
</html>
